package org.jeecg.common.util.blockchain;

import org.beykery.eth.WillWallet;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;

import java.math.BigInteger;

/**
 * check
 */
public class Main {
    /**
     * 检查所有账户的erc20持有情况
     *
     * @param args
     */
    public static final void main(String... args) throws Exception {
//    String privateKey = args[0];//这里填入你的eth私钥
//    String priceString = args[1];//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值
        // 29404234801161523843559422366258777649391763167937715829396789465576954978599
        //0x4abb1cbdeba4fd293c508442c59b3477ac551f1d
//        String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
//        String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2
//
//
//        WillWallet wallet = new WillWallet(privateKey);
//        String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
//        Web3j web3j = Web3j.build(new HttpService(url));
//        Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
//        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
//        System.out.println(clientVersion);
//        EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
//        System.out.println("账上eth余额 : " + bl.getBalance());
//
//        BigInteger gwei = new BigInteger("1000000000");
//        BigInteger price = gwei.multiply(new BigInteger(priceString));
//        BigInteger limit = new BigInteger("4800000");
//        RemoteCall<D___Jewellery_sol_Jewellery> call = D___Jewellery_sol_Jewellery.deploy(web3j, wallet.getCredentials(), price, limit);//TestToken_sol_TestToken可以改为你自己的合约
//        D___Jewellery_sol_Jewellery tt = call.send();
//        tt.m_TotalSupply().send();
//        System.out.println("部署合约 " + tt.getContractAddress() + " 成功 .");

      test3();
    }

    public static void test3() throws Exception {
        //        //调用合约
        String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
        String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2


        WillWallet wallet = new WillWallet(privateKey);
        String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
        Web3j web3j = Web3j.build(new HttpService(url));
        String contractAddress = "0x45e3719ea2316e021e6a605b0d74ac5dd623f7f4";
        BigInteger gwei = new BigInteger("1000000000");
        BigInteger price = gwei.multiply(new BigInteger(priceString));
        BigInteger limit = new BigInteger("4800000");
        D___Jewellery_sol_Jewellery contract = D___Jewellery_sol_Jewellery.load(contractAddress, web3j, wallet.getCredentials(), price, limit);
        TransactionReceipt transactionReceipt = contract.AddBox(new BigInteger("1"), "duping", "钻石项链").send();
        TransactionReceipt transactionReceipt1 = contract.m_TotalSupply().send();

        System.out.println("现在的值：" + transactionReceipt1.toString());
//        TransactionReceipt transactionReceipt = contract.set("nihaoshijie").send();
//        String value=contract.get().send();
//        System.out.println("现在的值："+value);
    }

    /**
     * 部署合约
     *
     * @param c           合约类
     * @param privateKey  私钥
     * @param priceString 价格（gwei）
     */
    public static void deploy(Class<? extends Contract> c, String privateKey, String priceString) throws Exception {
        WillWallet wallet = new WillWallet(privateKey);
        String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
        Web3j web3j = Web3j.build(new HttpService(url));
        Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
        System.out.println(clientVersion);
        EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
        System.out.println("账上eth余额 : " + bl.getBalance());

        BigInteger gwei = new BigInteger("1000000000");
        BigInteger price = gwei.multiply(new BigInteger(priceString));
        BigInteger limit = new BigInteger("4800000");
        RemoteCall<D___Jewellery_sol_Jewellery> call = D___Jewellery_sol_Jewellery.deploy(web3j, wallet.getCredentials(), price, limit);
        D___Jewellery_sol_Jewellery tt = call.send();
        System.out.println("部署合约 " + tt.getContractAddress() + " 成功 .");
    }


}
